package ihm;

import static javax.swing.GroupLayout.Alignment.LEADING;

import generation.ExtendedFile;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

/**
 * 
 * Fenetre des résultats 
 * 
 * Permet de visualiser les resultats reformatés avec la possibilité de les enregistrer dans un fichier
 *
 */
public class Resultats extends JFrame implements ActionListener{

	private JLabel label1;
	private JTextArea textArea;
	private JScrollPane panel1;
	private JPanel panel2;
	private JButton enregistrer;
	private JButton retour;
	private JButton fermer;
	
	/**
	 * Constructeur
	 */
	public Resultats(){
		this.setTitle("Resultats");
		Container conteneur = this.getContentPane();
		GroupLayout layout = new GroupLayout(conteneur);
		conteneur.setLayout(layout);
		layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        
		label1= new JLabel("Observations : ");
		textArea = new JTextArea(30,20);
		textArea.setEditable(false);
		panel1 = new JScrollPane(textArea);
		enregistrer = new JButton("Enregistrer");
		enregistrer.addActionListener(this);
		retour = new JButton("Retour Accueil");
		retour.addActionListener(this);
		fermer = new JButton("Fermer");
		fermer.addActionListener(this);
		
		panel2 = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		//natural height, maximum width
		 c.fill = GridBagConstraints.HORIZONTAL;
		 c.weightx = 0.5;
		 c.ipady = 20;
		 c.gridx = 0;
		 c.gridy = 0;
		 panel2.add(enregistrer, c);
		 c.gridx = 1;
		 c.insets = new Insets(0,25,0,0);
		 panel2.add(retour, c);
		 c.gridx = 2;
		 c.insets = new Insets(0,25,0,0);
		 c.anchor = GridBagConstraints.SOUTH;
		 panel2.add(fermer, c);
		
		 layout.setHorizontalGroup(layout.createSequentialGroup()
					.addGroup(layout.createParallelGroup(LEADING)
						.addComponent(label1)
						.addComponent(panel1)
						.addComponent(panel2)
						)		
				);
						
		 layout.setVerticalGroup(layout.createSequentialGroup()
						.addGap(50)
						.addComponent(label1)					
						.addComponent(panel1)
						.addGap(30)
						.addComponent(panel2)
						
			            
			     ); 
		 
		 
		
		this.pack();
		//tuer le processus en cliquant sur la croix pour fermer la fenetre
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);	
		
		Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		
		this.setLocation((screen.width - this.getSize().width)/2, 0);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object selected = e.getSource();
		if(selected == enregistrer){
//			JOptionPane.showMessageDialog(this, "Enregistrer");
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.showSaveDialog(this);
			String fileName = fileChooser.getSelectedFile().getAbsolutePath();
			new ExtendedFile(fileName).writeFile(textArea.getText());

		}else if (selected == retour){
			this.setVisible(false);
			Accueil accueil = new Accueil();
			accueil.setVisible(true);
		}else if (selected == fermer){
			System.exit(0);
		
		}

}

	/**
	 * @return textArea
	 */
	public JTextArea getTextArea() {
		return textArea;
	}

	/**
	 * @param textArea
	 */
	public void setTextArea(JTextArea textArea) {
		this.textArea = textArea;
	}
	
}
